package com.ruoyi.modules.appController;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.Result;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.service.TokenService;
import com.ruoyi.modules.domain.NumRecord;
import com.ruoyi.modules.domain.PayPayInfo;
import com.ruoyi.modules.service.IPayPayInfoService;
import com.ruoyi.modules.util.wechar.WeCharPayRequestUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jdom2.JDOMException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 支付信息Controller
 *
 * @author ruoyi
 * @date 2022-01-22
 */
@Api(tags = "qpp - 支付信息")
@RestController
@RequestMapping("/app/payPayInfo")
@Slf4j
public class AppPayPayInfoController extends BaseController {
    @Autowired
    private IPayPayInfoService payPayInfoService;
    @Autowired
    private TokenService tokenService;


    /**
     * 支付回调
     * @param request
     * @param response
     * @throws JDOMException
     * @throws IOException
     */
    @RequestMapping("/openApi/wx/callback")
    @ResponseBody
    public void wxCallback(HttpServletRequest request, HttpServletResponse response) throws JDOMException, IOException {
        WeCharPayRequestUtil.wxPayNotifyFun(request,response,(transaction_id, out_trade_no)->{
            return payPayInfoService.payCallback(transaction_id, out_trade_no);
        });
    }


    /**
     * 查询订单支付状态
     */
    @ApiOperation(value="查询订单支付状态", notes="查询订单支付状态")
    @GetMapping("/openApi/queryOrderFlag")
    public AjaxResult queryOrderFlag(HttpServletRequest req, String tradeNo)  {

        PayPayInfo payPayInfo =  payPayInfoService.getOne(new LambdaQueryWrapper<PayPayInfo>().eq(PayPayInfo::getTradeNo, tradeNo).last("limit 1"));
        if (payPayInfo == null){
            return error("payPayInfo 为空");
        }
        if (payPayInfo.getPaymentStatus() == 1){
            AjaxResult success = success("订单支付成功");
            success.put("pay_flag", true);
            return success;
        } else {
            AjaxResult error = error("订单状态待支付");
            error.put("pay_flag", false);
            return error;
        }
    }


    /**
     * 购买次数
     */
    @ApiOperation(value="购买次数", notes="购买次数")
    @GetMapping("/payDataNum")
    public Result payDataNum(HttpServletRequest req, Long topUpRuleId)  {
        Long agentUserId = tokenService.getUserId(req);
        PayPayInfo payPayInfo = payPayInfoService.payDataNum(agentUserId, topUpRuleId);
        try {
            Map<String, String> map = WeCharPayRequestUtil.wxUnifiedPay(payPayInfo.getTradeNo(), payPayInfo.getPayCash(), "真探运营");
            map.put("tradeNo", payPayInfo.getTradeNo());
            return Result.ok(map);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("下单失败");
        }
    }



}
